package com.eric.service.impl;

import cn.dev33.satoken.util.SaResult;
import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.eric.domain.dto.ApplicantDTO;
import com.eric.domain.po.Applicant;
import com.eric.domain.vo.ApplicantVO;
import com.eric.mapper.ApplicantMapper;
import com.eric.service.ApplicantService;
import com.mybatisflex.core.paginate.Page;
import com.mybatisflex.core.update.UpdateChain;
import com.mybatisflex.spring.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashSet;
import java.util.List;
import java.util.Objects;
import java.util.Set;

import static com.eric.domain.po.table.ApplicantTableDef.APPLICANT;

/**
 *  服务层实现。
 *
 * @author eric.deng
 * @since 2025-03-05
 */
@Service
public class ApplicantServiceImpl extends ServiceImpl<ApplicantMapper, Applicant>  implements ApplicantService{

    @Override
    @Transactional
    public SaResult saveOne(ApplicantVO applicantVO) {
        Applicant one = queryChain().select()
                .where(APPLICANT.APPLICANT_NAME.eq(applicantVO.getApplicantName()))
                .one();
        boolean empty = BeanUtil.isEmpty(one);
        if (empty){
            Applicant applicant=new Applicant();
            BeanUtil.copyProperties(applicantVO,applicant);
            boolean save = save(applicant);
            return save ? SaResult.ok() : (SaResult.error("录入错误"));
        }else {
            return SaResult.error("供应商已经存在！");
        }
    }

    @Override
    public SaResult updateByIdService(ApplicantDTO applicantDTO) {
        boolean applicantNameEffective=false;
        boolean professionalEffective=false;
        boolean applicantDescriptionEffective =false;

        if (applicantDTO.getId()==null){
            return SaResult.data("数据错误！");
        }
        if (!StrUtil.isBlank(applicantDTO.getApplicantName())){
            applicantNameEffective=true;
        }
        if (!StrUtil.isBlank(applicantDTO.getProfessional())){
            professionalEffective=true;
        }
        if (!StrUtil.isBlank(applicantDTO.getDescription())){
            applicantDescriptionEffective =true;
        }

        Applicant one = queryChain().where(APPLICANT.APPLICANT_NAME.eq(applicantDTO.getApplicantName())).one();
//        如果通过supplierName查到的one不为空，并且id和supplierDTO中的id不相等，就说明供应商名称重复
        if (!BeanUtil.isEmpty(one) &&!Objects.equals(one.getId(),applicantDTO.getId())){
            return SaResult.error("供应商名称已存在！");
        }

        boolean updated = UpdateChain.of(Applicant.class)
                .set(APPLICANT.APPLICANT_NAME, applicantDTO.getApplicantName(),applicantNameEffective)
                .set(APPLICANT.PROFESSIONAL,applicantDTO.getProfessional() ,professionalEffective)
                .set(APPLICANT.DESCRIPTION, applicantDTO.getDescription(), applicantDescriptionEffective)
                .where(APPLICANT.ID.eq(applicantDTO.getId()))
                .update();
        return SaResult.data(updated);
    }

    @Override
    public SaResult getListByPage(Integer cur, Integer pageSize) {
        if (cur==null||cur<=0){
            cur=1;
        }
        if (pageSize==null||pageSize<=0){
            pageSize=11;
        }

        List<Applicant> applicantList = queryChain().select()
                .from(APPLICANT).orderBy(APPLICANT.UPDATE_TIME.desc())
                .page(new Page<>(cur, pageSize))
                .getRecords();
        if (!applicantList.isEmpty()){
            List<ApplicantDTO> applicantDTOList = BeanUtil.copyToList(applicantList, ApplicantDTO.class);
            return SaResult.data(applicantDTOList);
        }


        return SaResult.error("没有数据！");
    }

    @Override
    public SaResult getApplicantListByNameService(String name, Integer cur, Integer pageSize) {
        if (cur==null||cur<=0){
            cur=1;
        }
        if (pageSize==null||pageSize<=0){
            pageSize=11;
        }

        List<Applicant> listByApplicantName = queryChain().select()
                .where(APPLICANT.APPLICANT_NAME.like(name, true))
                .orderBy(APPLICANT.UPDATE_TIME.desc())
                .page(new Page<>(cur, pageSize)).getRecords();
        List<Applicant> ListByProject = queryChain().select().where(APPLICANT.PROFESSIONAL.like(name, true))
                .orderBy(APPLICANT.UPDATE_TIME.desc())
                .page(new Page<>(cur, pageSize)).getRecords();
        Set<Applicant> applicantSet = new HashSet<>() ;

        if (!BeanUtil.isEmpty(listByApplicantName)){
            applicantSet.addAll(listByApplicantName);
        }
        if (!BeanUtil.isEmpty(ListByProject)){
            applicantSet.addAll(ListByProject);
        }

        if (applicantSet.isEmpty()){
            return SaResult.error("没有数据");
        }else{
            List<ApplicantDTO> applicantDTOList = BeanUtil.copyToList(applicantSet, ApplicantDTO.class);

            return SaResult.data(applicantDTOList);
        }
    }

    @Override
    public SaResult getApplicantListByNameCountService(String name) {
        List<Applicant> listByName = queryChain().select().where(APPLICANT.APPLICANT_NAME.like(name, true)).list();
        List<Applicant> listByProject = queryChain().select().where(APPLICANT.PROFESSIONAL.like(name, true)).list();
        Set<Applicant>applicantSet=new HashSet<>();
        applicantSet.addAll(listByName);
        applicantSet.addAll(listByProject);
        return SaResult.data(applicantSet.size());
    }
}
